using MicroMsg.Storage;
using MicroMsg.UI.Util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Animation;
namespace MicroMsg.UI.UserContrl
{
	public class ChatEggsLineItem : Button
	{
		private const int MAX_ELEMENT_NUM = 400;
		private Paragraph sParagraph;
		private ChatEggsLineItem mLastItem;
		private int flashCount;
		public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ChatEggsLineItem), new PropertyMetadata("", null));
		private string lastTextCache;
		private bool _contentLoaded;
		public event EventHandler mAniCompleted;
		public string Text
		{
			get
			{
				return (string)base.GetValue(ChatEggsLineItem.TextProperty);
			}
			set
			{
				base.SetValue(ChatEggsLineItem.TextProperty, value);
				this.RefreshText();
			}
		}
		public ChatEggsLineItem() : this(false)
		{
		}
		public ChatEggsLineItem(bool isLastest)
		{
			this.InitializeComponent();
			base.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			base.set_Opacity(0.0);
			if (isLastest)
			{
				base.add_Loaded(new RoutedEventHandler(this.EggsLineItem_Loaded));
			}
		}
		private void EggsLineItem_Loaded(object sender, RoutedEventArgs e)
		{
			this.startAniToShow();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.sParagraph = (base.GetTemplateChild("sParagraph") as Paragraph);
			this.RefreshText();
		}
		public void InitData(EmojiArt emojiart, ChatEggsLineItem lastItem, EventHandler aniCompleted)
		{
			if (emojiart == null)
			{
				return;
			}
			this.Text = emojiart.strEmojiArt;
			this.mLastItem = lastItem;
			this.mAniCompleted = aniCompleted;
		}
		public void startAniToShow()
		{
			Storyboard storyboard = new Storyboard();
			DoubleAnimation doubleAnimation = ToolFunction.createAnimation(new double?(0.0), 1.0, this, "Opacity", 90u);
			doubleAnimation.add_Completed(new EventHandler(this.aniToShow_Completed));
			storyboard.get_Children().Add(doubleAnimation);
			storyboard.Begin();
		}
		public void flashEggs()
		{
			this.flashCount++;
			uint duration = 550u;
			double num;
			double double_;
			if (this.flashCount % 2 == 0)
			{
				num = 0.4;
				double_ = 1.0;
			}
			else
			{
				num = 1.0;
				double_ = 0.4;
			}
			if (this.flashCount == 5)
			{
				num = 1.0;
				double_ = 0.0;
				duration = 1400u;
			}
			Storyboard storyboard = new Storyboard();
			DoubleAnimation doubleAnimation = ToolFunction.createAnimation(new double?(num), double_, this, "Opacity", duration);
			storyboard.get_Children().Add(doubleAnimation);
			doubleAnimation.add_Completed(new EventHandler(this.flashEggs_Completed));
			storyboard.Begin();
		}
		private void flashEggs_Completed(object sender, EventArgs e)
		{
			if (this.flashCount < 5)
			{
				this.flashEggs();
				return;
			}
			this.flashCount = 0;
			this.mLastItem = null;
			if (this.mAniCompleted != null)
			{
				this.mAniCompleted.Invoke(this, new EventArgs());
			}
		}
		private void aniToShow_Completed(object sender, EventArgs e)
		{
			if (this.mLastItem != null)
			{
				this.mLastItem.startAniToShow();
				return;
			}
			if (this.mAniCompleted != null)
			{
				this.mAniCompleted.Invoke(this, null);
			}
		}
		private void RefreshText()
		{
			if (this.sParagraph == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(this.Text))
			{
				this.sParagraph.get_Inlines().Clear();
				return;
			}
			if (this.Text == this.lastTextCache)
			{
				return;
			}
			this.lastTextCache = this.Text;
			this.sParagraph.get_Inlines().Clear();
			List<InLineResult> inLineResult = InlineElementHelper.GetInLineResult(this.lastTextCache);
			int num = 0;
			using (List<InLineResult>.Enumerator enumerator = inLineResult.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					InLineResult current = enumerator.get_Current();
					if (current.mInline != null)
					{
						num++;
						this.sParagraph.get_Inlines().Add(current.mInline);
						if (num >= 400)
						{
							Run run = new Run();
							run.set_Text("......");
							this.sParagraph.get_Inlines().Add(run);
							break;
						}
					}
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/ChatEggsLineItem.xaml", 2));
		}
	}
}
